package com.gym.code.config.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gym.code.config.admin.bean.ColumnMappingBean;
import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.bean.table.TableBaseMappingBean;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.client.DBHelpService;
import com.gym.code.config.admin.client.model.DBColumnModel;
import com.gym.code.config.admin.client.model.DBDatabaseModel;
import com.gym.code.config.admin.client.model.DBTableModel;
import com.gym.code.config.admin.service.IColumnMappingService;
import com.gym.code.config.admin.service.IDBMappingService;
import com.gym.code.config.admin.service.ITableMappingService;
import com.gym.code.generator.EnumContants.FieldClassType;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 */
@Controller
@RequestMapping("/field")
public class FieldController {
	private static final Logger logger = LoggerFactory.getLogger(FieldController.class);

	@Autowired
	private IDBMappingService dbService;
	
	@Autowired
	private ITableMappingService tableMappingService;
	
	@Autowired
	private DBHelpService dbHelpService;
	
	@Autowired
	private IColumnMappingService  columnMappingService;
	
	@RequestMapping("/add")
	public ModelAndView add(long tableId,HttpServletRequest request) {
		logger.info("add column mapping ");
		TableMappingBean bean = tableMappingService.query(tableId);
		DBBaseMappingInfo baseDbBean = dbService.queryBase(bean.getDbId());
		DBDatabaseModel dbModel = DBController.wrapperDBTableModel(baseDbBean);
		Map<String,DBTableModel> dbTableMap  = dbHelpService.getTableInfoMap(dbModel);
		DBTableModel dbTableModel = dbTableMap.get(bean.getTableName());
		List<DBColumnModel> columnList = dbTableModel.getColumnList();
		
		List<ColumnMappingBean> columnBeanList = new ArrayList<ColumnMappingBean>();
		for(DBColumnModel item : columnList){
			ColumnMappingBean columnBean = new ColumnMappingBean();
			columnBean.setColumnName(item.getColumnName());
			columnBean.setPropertyName(request.getParameter(item.getColumnName()+"_fieldName")); 
			//枚举处理
			if(request.getParameter(item.getColumnName()+"_isEnum") != null && StringUtils.equals(request.getParameter(item.getColumnName()+"_isEnum"), "1") ){
				columnBean.setPatternValue(request.getParameter(item.getColumnName()+"_enumPattern"));
				columnBean.setEnumTypeName(request.getParameter(item.getColumnName()+"_enumName"));
				columnBean.setEnumFlag(1);
				columnBean.setValueType(request.getParameter(item.getColumnName()+"_enumName"));
			} else {
				columnBean.setValueType(item.getClzStr());
			}
			columnBean.setDefaultValue(request.getParameter(item.getColumnName()+"_defaultValue"));
			columnBean.setTableId(tableId);
			columnBeanList.add(columnBean);
		}
		columnMappingService.insert(columnBeanList);
		return new ModelAndView("redirect:/table/view.do?tableId=" + bean.getId() + "&tableName=" + bean.getTableName());
	}
	
	
	@RequestMapping("/pUpdate")
	public ModelAndView pUpdate(long columnId) {
		ColumnMappingBean columnBean = columnMappingService.query(columnId);
		return new ModelAndView("/field/update","bean",columnBean);
	}
	
	
	@RequestMapping("/update")
	public ModelAndView update(long id,long tableId,String propertyName,int enumFlag, String defaultValue,String valueType,String enumTypeName,String patternValue,int scope) {
		ColumnMappingBean bean = new ColumnMappingBean();
		bean.setId(id);
		bean.setPropertyName(propertyName);
		bean.setDefaultValue(defaultValue);
		if(enumFlag == 1) {
			bean.setValueType(FieldClassType.instance(enumTypeName));
			bean.setEnumFlag(enumFlag);
			bean.setEnumTypeName(enumTypeName);
			bean.setPatternValue(patternValue);
		}
		
		columnMappingService.update(bean);
		bean.setScope(scope);
		
		TableBaseMappingBean tableBean = tableMappingService.queryBase(tableId);
		return new ModelAndView("redirect:/table/view.do?tableId=" + tableId + "&tableName=" + tableBean.getTableName());
	}
	
}
